﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using System.Data;

namespace TierApplication
{
/// <summary>
	/// 类P_Section。
	/// </summary>
	public class P_Section
	{
		public P_Section()
		{}
		#region Model
		private string _sectionid;
		private string _sectionname;
		private string _prevsectionid;
		private string _section;
		private string _sectiontype;
		private string _sectionmanager;
		private string _sectionphone;
		/// <summary>
		/// 
		/// </summary>
		public string SectionID
		{
			set{ _sectionid=value;}
			get{return _sectionid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string SectionName
		{
			set{ _sectionname=value;}
			get{return _sectionname;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string PrevSectionID
		{
			set{ _prevsectionid=value;}
			get{return _prevsectionid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string Section
		{
			set{ _section=value;}
			get{return _section;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string SectionType
		{
			set{ _sectiontype=value;}
			get{return _sectiontype;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string SectionManager
		{
			set{ _sectionmanager=value;}
			get{return _sectionmanager;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string SectionPhone
		{
			set{ _sectionphone=value;}
			get{return _sectionphone;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public P_Section(string sectionID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append("SectionID,SectionName,PrevSectionID,Section,SectionType,SectionManager,SectionPhone ");
			strSql.Append(" from P_Section ");
            strSql.Append(" where SectionID='" + sectionID + "'");
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				SectionID=ds.Tables[0].Rows[0]["SectionID"].ToString();
				SectionName=ds.Tables[0].Rows[0]["SectionName"].ToString();
				PrevSectionID=ds.Tables[0].Rows[0]["PrevSectionID"].ToString();
				Section=ds.Tables[0].Rows[0]["Section"].ToString();
				SectionType=ds.Tables[0].Rows[0]["SectionType"].ToString();
				SectionManager=ds.Tables[0].Rows[0]["SectionManager"].ToString();
				SectionPhone=ds.Tables[0].Rows[0]["SectionPhone"].ToString();
			}
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string SectionID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from P_Section where SectionID='"+SectionID+"'"  );
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [P_Section](");
			strSql.Append("SectionID,SectionName,PrevSectionID,Section,SectionType,SectionManager,SectionPhone");
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append("'"+SectionID+"',");
			strSql.Append("'"+SectionName+"',");
			strSql.Append("'"+PrevSectionID+"',");
			strSql.Append("'"+Section+"',");
			strSql.Append("'"+SectionType+"',");
			strSql.Append("'"+SectionManager+"',");
			strSql.Append("'"+SectionPhone+"'");
			strSql.Append(")");
			DbHelperSQL.ExecuteSql(strSql.ToString());
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update P_Section set ");
			strSql.Append("SectionName='"+SectionName+"',");
			strSql.Append("PrevSectionID='"+PrevSectionID+"',");
			strSql.Append("Section='"+Section+"',");
			strSql.Append("SectionType='"+SectionType+"',");
			strSql.Append("SectionManager='"+SectionManager+"',");
			strSql.Append("SectionPhone='"+SectionPhone+"'");
			strSql.Append(" where SectionID='"+SectionID+"' ");
			DbHelperSQL.ExecuteSql(strSql.ToString());
		}


		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(string SectionID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from P_Section ");
			strSql.Append(" where SectionID='"+SectionID+"'");
				DbHelperSQL.ExecuteSql(strSql.ToString());
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(string sectionID)
		{

			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" SectionID,SectionName,PrevSectionID,Section,SectionType,SectionManager,SectionPhone ");
			strSql.Append(" from P_Section ");
            strSql.Append(" where SectionID='" + sectionID + "' ");
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				SectionID=ds.Tables[0].Rows[0]["SectionID"].ToString();
				SectionName=ds.Tables[0].Rows[0]["SectionName"].ToString();
				PrevSectionID=ds.Tables[0].Rows[0]["PrevSectionID"].ToString();
				Section=ds.Tables[0].Rows[0]["Section"].ToString();
				SectionType=ds.Tables[0].Rows[0]["SectionType"].ToString();
				SectionManager=ds.Tables[0].Rows[0]["SectionManager"].ToString();
				SectionPhone=ds.Tables[0].Rows[0]["SectionPhone"].ToString();
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select a.*,(Select UserName from P_User where UserID=a.SectionManager) SectionManagerName ");
			strSql.Append(" FROM P_Section a");
			if(strWhere.Trim()!="")
			{

				strSql.Append(" where 1=1 "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

        public bool IsExistsUser(string sSectionID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from P_User where SectionID='" + sSectionID + "'");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        public static DataTable GetAllSection()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [SectionID] ID,[SectionName] Name,PrevSectionID PID ");
            strSql.Append(" FROM P_Section a");
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        public static DataTable GetAllSection(string sSectionID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [SectionID] ID,[SectionName] Name,PrevSectionID PID ");
            strSql.Append(" FROM P_Section a");
            strSql.Append(" where SectionID in (Select SectionID from f_SectionID('" + sSectionID +"'))");
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        public static DataTable GetAllSectionByDeptID(string sDeptID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" Select SectionID ID,  ");
            strSql.Append(" (Select PrevSectionID from P_Section where SectionID=a.SectionID) PID,  ");
            strSql.Append(" (Select SectionName from P_Section where SectionID=a.SectionID) Name  ");
            strSql.Append(" from P_Dept_Section a where 1=1 ");
            if (sDeptID != "")
            {
                strSql.Append(" and DeptID='" + sDeptID + "'");
            }
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }
		#endregion  Method
	}
}

